package com.xujiang.common_core.util.ourexception;

/**
 * @author xujiang
 * @version 2017-06-30 17:32
 */
public class SystemRuntimeException extends RuntimeException implements ISystemException {

    private SystemExceptionHelper mExceptionHelper;

    public SystemRuntimeException(String errorMessage, Throwable e) {  //全部最终以此初始化
        super(errorMessage);
        mExceptionHelper = SystemExceptionHelper.with(e);
    }

    public SystemRuntimeException(Throwable e) {
        this(e.getMessage(), e);
    }

    /**
     * 打印原因异常或者可以说成message
     */
    @Override
    public synchronized Throwable getCause() {
        if (mExceptionHelper == null) {
            return super.getCause();
        }
        return mExceptionHelper.getCause();
    }

    /**
     * 直接得到默认的RuntimeException的cause
     */
    public Throwable getOriginalCause() {
        return super.getCause();
    }

    /**
     * 打印
     *
     */
    @Override
    public String toString() {
        if (mExceptionHelper == null || mExceptionHelper.getmThrowable() == null) {
            return super.toString();
        }
        return mExceptionHelper.throwableToString(this);
    }

    public Throwable getThrowable() {
        return mExceptionHelper.getmThrowable();
    }

    public void setThrowable(Throwable throwable) {
        mExceptionHelper.setmThrowable(throwable);
    }

}
